#include <buola/scene/geodes/ccube.h>
#include <buola/scene/cmesh.h>
#include <buola/scene/cvertexdata.h>
#include <buola/scene/cindexdata.h>
#include <buola/scene/cmeshbuilder.h>
#include <BulletCollision/CollisionShapes/btBoxShape.h>

namespace buola { namespace scene {

static PMesh cube_mesh(double pX,double pY,double pZ)
{
/*    
    PMesh lMesh=new CMesh;
    
    std::vector<mat::CVec3f> lPositions;

    lPositions.emplace_back(-1.0,-1.0,-1.0);
    lPositions.emplace_back( 1.0,-1.0,-1.0);
    lPositions.emplace_back( 1.0, 1.0,-1.0);
    lPositions.emplace_back(-1.0, 1.0,-1.0);
    lPositions.emplace_back(-1.0,-1.0, 1.0);
    lPositions.emplace_back( 1.0,-1.0, 1.0);
    lPositions.emplace_back( 1.0, 1.0, 1.0);
    lPositions.emplace_back(-1.0, 1.0, 1.0);

    std::vector<mat::CVec3f> lNormals;

    lNormals.emplace_back( 1.0, 0.0, 0.0);
    lNormals.emplace_back(-1.0, 0.0, 0.0);
    lNormals.emplace_back( 0.0, 1.0, 0.0);
    lNormals.emplace_back( 0.0,-1.0, 0.0);
    lNormals.emplace_back( 0.0, 0.0, 1.0);
    lNormals.emplace_back( 0.0, 0.0,-1.0);

    std::vector<CMesh::SVertex> lVertices;

    lVertices.emplace_back(1,0);
    lVertices.emplace_back(2,0);
    lVertices.emplace_back(5,0);
    lVertices.emplace_back(6,0);
    lVertices.emplace_back(nRestartPrimitive);
    lVertices.emplace_back(0,1);
    lVertices.emplace_back(3,1);
    lVertices.emplace_back(4,1);
    lVertices.emplace_back(7,1);
    lVertices.emplace_back(nRestartPrimitive);
    lVertices.emplace_back(2,2);
    lVertices.emplace_back(3,2);
    lVertices.emplace_back(6,2);
    lVertices.emplace_back(7,2);
    lVertices.emplace_back(nRestartPrimitive);
    lVertices.emplace_back(0,3);
    lVertices.emplace_back(1,3);
    lVertices.emplace_back(4,3);
    lVertices.emplace_back(5,3);
    lVertices.emplace_back(nRestartPrimitive);
    lVertices.emplace_back(4,4);
    lVertices.emplace_back(5,4);
    lVertices.emplace_back(7,4);
    lVertices.emplace_back(6,4);
    lVertices.emplace_back(nRestartPrimitive);
    lVertices.emplace_back(0,5);
    lVertices.emplace_back(1,5);
    lVertices.emplace_back(3,5);
    lVertices.emplace_back(2,5);
    lVertices.emplace_back(nRestartPrimitive);

    lMesh->CreateSubMesh("cube",lPositions,lNormals,lVertices);
*/

    std::vector<mat::CVec3d> lPositions;

    lPositions.emplace_back(-pX/2,-pY/2,-pZ/2);
    lPositions.emplace_back( pX/2,-pY/2,-pZ/2);
    lPositions.emplace_back( pX/2, pY/2,-pZ/2);
    lPositions.emplace_back(-pX/2, pY/2,-pZ/2);
    lPositions.emplace_back(-pX/2,-pY/2, pZ/2);
    lPositions.emplace_back( pX/2,-pY/2, pZ/2);
    lPositions.emplace_back( pX/2, pY/2, pZ/2);
    lPositions.emplace_back(-pX/2, pY/2, pZ/2);

    std::vector<mat::CVec3d> lNormals;

    lNormals.emplace_back( 1.0, 0.0, 0.0);
    lNormals.emplace_back(-1.0, 0.0, 0.0);
    lNormals.emplace_back( 0.0, 1.0, 0.0);
    lNormals.emplace_back( 0.0,-1.0, 0.0);
    lNormals.emplace_back( 0.0, 0.0, 1.0);
    lNormals.emplace_back( 0.0, 0.0,-1.0);

    std::vector<int> lVPos,lVNorm;

    lVPos.emplace_back(1);
    lVNorm.emplace_back(0);
    lVPos.emplace_back(2);
    lVNorm.emplace_back(0);
    lVPos.emplace_back(5);
    lVNorm.emplace_back(0);
    lVPos.emplace_back(6);
    lVNorm.emplace_back(0);

    lVPos.emplace_back(0);
    lVNorm.emplace_back(1);
    lVPos.emplace_back(3);
    lVNorm.emplace_back(1);
    lVPos.emplace_back(4);
    lVNorm.emplace_back(1);
    lVPos.emplace_back(7);
    lVNorm.emplace_back(1);

    lVPos.emplace_back(2);
    lVNorm.emplace_back(2);
    lVPos.emplace_back(3);
    lVNorm.emplace_back(2);
    lVPos.emplace_back(6);
    lVNorm.emplace_back(2);
    lVPos.emplace_back(7);
    lVNorm.emplace_back(2);

    lVPos.emplace_back(0);
    lVNorm.emplace_back(3);
    lVPos.emplace_back(1);
    lVNorm.emplace_back(3);
    lVPos.emplace_back(4);
    lVNorm.emplace_back(3);
    lVPos.emplace_back(5);
    lVNorm.emplace_back(3);

    lVPos.emplace_back(4);
    lVNorm.emplace_back(4);
    lVPos.emplace_back(5);
    lVNorm.emplace_back(4);
    lVPos.emplace_back(7);
    lVNorm.emplace_back(4);
    lVPos.emplace_back(6);
    lVNorm.emplace_back(4);

    lVPos.emplace_back(0);
    lVNorm.emplace_back(5);
    lVPos.emplace_back(1);
    lVNorm.emplace_back(5);
    lVPos.emplace_back(3);
    lVNorm.emplace_back(5);
    lVPos.emplace_back(2);
    lVNorm.emplace_back(5);

    std::vector<int> lFaces;
    lFaces.push_back(0);
    lFaces.push_back(4);
    lFaces.push_back(8);
    lFaces.push_back(12);
    lFaces.push_back(16);
    lFaces.push_back(20);
    lFaces.push_back(24);

    std::vector<int> lSubMesh;
    lSubMesh.push_back(0);
    lSubMesh.push_back(1);
    lSubMesh.push_back(2);
    lSubMesh.push_back(3);
    lSubMesh.push_back(4);
    lSubMesh.push_back(5);

    CMeshBuilder lBuilder;

    lBuilder.SetPosition(lPositions);
    lBuilder.SetNormal(lNormals);

    lBuilder.SetVertices(lVPos,lVNorm);

    lBuilder.SetFaces(lFaces);

    lBuilder.AddSubMesh("cube",lSubMesh,EPrimitive::TRIANGLE_STRIP);

    return lBuilder.BuildMesh();
}

CCube::CCube(double pX,double pY,double pZ)
    :   CGeode(cube_mesh(pX,pY,pZ))
    ,   mSizes(pX,pY,pZ)
{
}

CCube::~CCube()
{
}

PBulletCollisionShape CCube::GetCollisionShape()
{
    return PBulletCollisionShape(new btBoxShape(btVector3(mSizes.x()/2,mSizes.y()/2,mSizes.z()/2)));
}

/*namespace gl*/ } /*namespace buola*/ }
